/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#pragma once

#include <kmhdfs/common/hash.h>
#include <string>
#include <sstream>
#define INVALID_POOL_ID "bp_invalid_id"

namespace Hdfs {
namespace Internal {

/**
 * Identifies a Block uniquely across the block pools
 */
class ExtendedBlock {
public:
    ExtendedBlock() :
        blockId(0), generationStamp(0), numBytes(0), poolId(INVALID_POOL_ID) {
    }

    int64_t getBlockId() const {
        return blockId;
    }

    void setBlockId(int64_t blockId) {
        this->blockId = blockId;
    }

    int64_t getGenerationStamp() const {
        return generationStamp;
    }

    void setGenerationStamp(int64_t generationStamp) {
        this->generationStamp = generationStamp;
    }

    int64_t getNumBytes() const {
        return numBytes;
    }

    void setNumBytes(int64_t numBytes) {
        this->numBytes = numBytes;
    }

    const std::string & getPoolId() const {
        return poolId;
    }

    void setPoolId(const std::string & poolId) {
        this->poolId = poolId;
    }

    const std::string toString() const {
        std::stringstream ss;
        ss.imbue(std::locale::classic());
        ss << "[block pool ID: " << poolId << " block ID " << blockId << "_"
           << generationStamp << "]";
        return ss.str();
    }

    size_t hash_value() const {
        size_t values[] = { Int64Hasher(blockId), StringHasher(poolId) };
        return CombineHasher(values, sizeof(values) / sizeof(values[0]));
    }

private:
    int64_t blockId;
    int64_t generationStamp;
    int64_t numBytes;
    std::string poolId;
};

}
}

HDFS_HASH_DEFINE(::Hdfs::Internal::ExtendedBlock);
